package org.junwei.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 扩展common-lang3 StringUtils工具类
 * @author weijunbin
 * @date 2018-08-01
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{
	private static Pattern linePattern = Pattern.compile("_(\\w)");
	private static Pattern humpPattern = Pattern.compile("[A-Z0-9]");

	/**
	 * 把字符串数组转为数据库in字符串 如a,b,c转为'a','b','c'
	 * @param strs
	 * @return
	 */
	public static String strToSqlIn(String[] strs){
		if(strs == null)
			return "";
		String returnStr = "";
		for(int i=0; i<strs.length; i++){
			if(i == strs.length -1){
				returnStr += "'" + strs[i] + "'";
			}else{
				returnStr += "'" + strs[i] + "',";
			}
		}
		return returnStr;
	}
	
	/**下划线转驼峰*/
    public static String lineToHump(String str){
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
    
    /**驼峰转下划线*/
    public static String humpToLine(String str){
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()){
            matcher.appendReplacement(sb, "_"+matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
